package com.goodwill.hdr.Strategy;


import com.goodwill.hdr.vo.PropertyFilter;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class SingleColumnValueFilterBuilder {

  public static   SingleColumnValueFilter buildWithRegComparator(PropertyFilter propertyFilter, CompareOperator op) {
        String[] values =propertyFilter.getPropertyValue().split(",");
        StringBuilder regx = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            if (i > 0) {
                regx.append("|");
            }
            regx.append("(^").append(values[i]).append("$)");
        }
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                Bytes.toBytes("cf"),
                Bytes.toBytes(propertyFilter.getPropertyName()),
                op,
                new RegexStringComparator(regx.toString()));
        return singleColumnValueFilter;
    }

    public static   SingleColumnValueFilter buildWithBinaryComparator(PropertyFilter propertyFilter, CompareOperator op){
       SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                Bytes.toBytes("cf"),
                Bytes.toBytes(propertyFilter.getPropertyName()),
                op,
                Bytes.toBytes(propertyFilter.getPropertyValue())
        );
       return singleColumnValueFilter;
    }

}
